package com.lframework.starter.web.config;

import com.lframework.starter.web.core.aop.*;
import com.lframework.starter.web.core.components.cache.CacheVariables;
import com.lframework.starter.web.core.components.generator.handler.impl.*;
import com.lframework.starter.web.core.components.resp.InvokeResultErrorBuilderWrapper;
import com.lframework.starter.web.core.components.resp.ResponseErrorBuilder;
import com.lframework.starter.web.core.components.security.UserTokenResolverImpl;
import com.lframework.starter.web.core.components.sign.CheckSignFactory;
import com.lframework.starter.web.core.components.sign.DefaultCheckSignFactory;
import com.lframework.starter.web.core.components.upload.handler.SecurityUploadHandler;
import com.lframework.starter.web.core.components.upload.handler.UploadHandler;
import com.lframework.starter.web.core.components.upload.handler.impl.*;
import com.lframework.starter.web.core.handlers.exception.WebExceptionHandler;
import com.lframework.starter.web.inner.components.oplog.AuthOpLogType;
import com.lframework.starter.web.inner.components.oplog.OtherOpLogType;
import com.lframework.starter.web.inner.components.oplog.SystemOpLogType;
import com.lframework.starter.web.inner.components.permission.OrderDataPermissionDataPermissionType;
import com.lframework.starter.web.inner.components.permission.ProductDataPermissionDataPermissionType;
import com.lframework.starter.web.inner.components.timeline.*;
import com.lframework.starter.web.inner.controller.AuthController;
import com.lframework.starter.web.inner.controller.QrtzController;
import com.lframework.starter.web.inner.controller.UserCenterController;
import com.lframework.starter.web.inner.controller.system.*;
import com.lframework.starter.web.inner.enums.system.SysDeptNodeType;
import com.lframework.starter.web.inner.handlers.exception.*;
import com.lframework.starter.web.inner.impl.*;
import com.lframework.starter.web.inner.impl.system.*;
import com.lframework.starter.web.inner.listeners.OpLogTimerListener;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;

@Slf4j
@Configuration
@Import({
        WebExceptionHandler.class,
        DicCityServiceImpl.class,
        GenerateCodeServiceImpl.class,
        OpLogsServiceImpl.class,
        OrderTimeLineServiceImpl.class,
        RecursionMappingServiceImpl.class,
        SecurityUploadRecordServiceImpl.class,
        DefaultSysSelectorController.class,
        OpLogController.class,
        SysDataDicCategoryController.class,
        SysDataDicController.class,
        SysDataDicItemController.class,
        SysDataPermissionDataController.class,
        SysDataPermissionModelDetailController.class,
        SysDeptController.class,
        SysGenerateCodeController.class,
        SysMailMessageController.class,
        SysMenuController.class,
        SysModuleController.class,
        SysNoticeController.class,
        SysNotifyGroupController.class,
        SysOpenDomainController.class,
        SysParameterController.class,
        SysRoleCategoryController.class,
        SysRoleController.class,
        SysRoleMenuController.class,
        SysSiteMessageController.class,
        SysUserController.class,
        SysUserGroupController.class,
        SysUserRoleController.class,
        TenantController.class,
        AuthController.class,
        QrtzController.class,
        UserCenterController.class,
        SysDataDicCategoryServiceImpl.class,
        SysDataDicItemServiceImpl.class,
        SysDataDicServiceImpl.class,
        SysDataPermissionDataServiceImpl.class,
        SysDataPermissionModelDetailServiceImpl.class,
        SysDeptServiceImpl.class,
        SysGenerateCodeServiceImpl.class,
        SysMailMessageServiceImpl.class,
        SysMenuServiceImpl.class,
        SysNoticeLogServiceImpl.class,
        SysNoticeServiceImpl.class,
        SysNoticeServiceImpl.ReloadNoticeListener.class,
        SysNotifyGroupReceiverServiceImpl.class,
        SysNotifyGroupServiceImpl.class,
        SysOpenDomainServiceImpl.class,
        SysParameterServiceImpl.class,
        SysRoleCategoryServiceImpl.class,
        SysRoleMenuServiceImpl.class,
        SysRoleServiceImpl.class,
        SysSiteMessageServiceImpl.class,
        SysSiteMessageServiceImpl.ReloadSiteMessageListener.class,
        SysUserDeptServiceImpl.class,
        SysUserGroupDetailServiceImpl.class,
        SysUserGroupServiceImpl.class,
        SysUserRoleServiceImpl.class,
        SysUserServiceImpl.class,
        SysUserTelephoneServiceImpl.class,
        QrtzServiceImpl.class,
        SysModuleServiceImpl.class,
        SysModuleTenantServiceImpl.class,
        TenantServiceImpl.class,
        DefaultUserDetailsService.class,
        SysDeptLogoController.class,
        SysDeptLogoServiceImpl.class,
        SysUploadController.class
})
@MapperScan("com.lframework.starter.web.**.mappers")
public class WebBeanAutoConfiguration implements EnvironmentAware {

    @Bean
    public ControllerAspector controllerAspector() {

        return new ControllerAspector();
    }

    @Bean
    public OpenApiAspect openApiAspect() {
        return new OpenApiAspect();
    }

    @Bean
    public PermissionAspect permissionAspect() {
        return new PermissionAspect();
    }

    @Bean
    public UploadHandler cosUploadHandler() {
        return new CosUploadHandler();
    }

    @Bean
    public UploadHandler localUploadHandler() {
        return new LocalUploadHandler();
    }

    @Bean
    public UploadHandler obsUploadHandler() {
        return new ObsUploadHandler();
    }

    @Bean
    public UploadHandler ossUploadHandler() {
        return new OssUploadHandler();
    }

    @Bean
    public SecurityUploadHandler ossSecurityUploadHandler() {
        return new OssSecurityUploadHandler();
    }

    @Bean
    public SecurityUploadHandler cosSecurityUploadHandler() {
        return new CosSecurityUploadHandler();
    }

    @Bean
    public SecurityUploadHandler localSecurityUploadHandler() {
        return new LocalSecurityUploadHandler();
    }

    @Bean
    public SecurityUploadHandler obsSecurityUploadHandler() {
        return new ObsSecurityUploadHandler();
    }

    @Bean
    public ResponseErrorBuilder invokeResultBuilderWrapper() {
        return new InvokeResultErrorBuilderWrapper();
    }

    @Bean
    public CheckSignFactory defaultCheckSignFactory() {
        return new DefaultCheckSignFactory();
    }

    @Bean("cacheVariables")
    public CacheVariables cacheVariables() {
        return new CacheVariables();
    }

    @Bean
    public UserTokenResolverImpl userTokenResolver() {
        return new UserTokenResolverImpl();
    }

    @Bean
    public CurrentDateTimeRuleGenerateCodeHandler currentDateTimeRuleGenerateHandler() {
        return new CurrentDateTimeRuleGenerateCodeHandler();
    }

    @Bean
    public CustomRandomStrGenerateCodeRuleHandler customRandomStrGenerateRuleHandler() {
        return new CustomRandomStrGenerateCodeRuleHandler();
    }

    @Bean
    public FlowGenerateCodeRuleHandler flowGenerateRuleHandler() {
        return new FlowGenerateCodeRuleHandler();
    }

    @Bean
    public SnowFlakeGenerateCodeRuleHandler snowFlakeGenerateRuleHandler() {
        return new SnowFlakeGenerateCodeRuleHandler();
    }

    @Bean
    public StaticStrGenerateCodeRuleHandler staticStrGenerateRuleHandler() {
        return new StaticStrGenerateCodeRuleHandler();
    }

    @Bean
    public UUIDGenerateCodeRuleHandler uuidGenerateRuleHandler() {
        return new UUIDGenerateCodeRuleHandler();
    }

    @Bean
    public OrderDataPermissionDataPermissionType orderDataPermissionDataPermissionType() {
        return new OrderDataPermissionDataPermissionType();
    }

    @Bean
    public ProductDataPermissionDataPermissionType productDataPermissionDataPermissionType() {
        return new ProductDataPermissionDataPermissionType();
    }

    @Bean
    public ApprovePassOrderTimeLineBizType approvePassOrderTimeLineBizType() {
        return new ApprovePassOrderTimeLineBizType();
    }

    @Bean
    public ApproveReturnOrderTimeLineBizType approveReturnOrderTimeLineBizType() {
        return new ApproveReturnOrderTimeLineBizType();
    }

    @Bean
    public CancelApproveOrderTimeLineBizType cancelApproveOrderTimeLineBizType() {
        return new CancelApproveOrderTimeLineBizType();
    }

    @Bean
    public CreateOrderTimeLineBizType createOrderTimeLineBizType() {
        return new CreateOrderTimeLineBizType();
    }

    @Bean
    public NormalOrderTimeLineBizType normalOrderTimeLineBizType() {
        return new NormalOrderTimeLineBizType();
    }

    @Bean
    public SendOrderTimeLineBizType sendOrderTimeLineBizType() {
        return new SendOrderTimeLineBizType();
    }

    @Bean
    public UpdateOrderTimeLineBizType updateOrderTimeLineBizType() {
        return new UpdateOrderTimeLineBizType();
    }

    @Bean
    public AccessDeniedExceptionConverter accessDeniedExceptionConverter() {
        return new AccessDeniedExceptionConverter();
    }

    @Bean
    public BindExceptionConverter bindExceptionConverter() {
        return new BindExceptionConverter();
    }

    @Bean
    public ClientExceptionConverter clientExceptionConverter() {
        return new ClientExceptionConverter();
    }

    @Bean
    public ConstraintViolationExceptionConverter constraintViolationExceptionConverter() {
        return new ConstraintViolationExceptionConverter();
    }

    @Bean
    public ExceptionConverter exceptionConverter() {
        return new ExceptionConverter();
    }

    @Bean
    public FileUploadExceptionConverter fileUploadExceptionConverter() {
        return new FileUploadExceptionConverter();
    }

    @Bean
    public HttpMessageNotReadableExceptionConverter httpMessageNotReadableExceptionConverter() {
        return new HttpMessageNotReadableExceptionConverter();
    }

    @Bean
    public HttpRequestMethodNotSupportedExceptionConverter httpRequestMethodNotSupportedExceptionConverter() {
        return new HttpRequestMethodNotSupportedExceptionConverter();
    }

    @Bean
    public MethodArgumentNotValidExceptionConverter methodArgumentNotValidExceptionConverter() {
        return new MethodArgumentNotValidExceptionConverter();
    }

    @Bean
    public NotPermissionExceptionConverter notPermissionExceptionConverter() {
        return new NotPermissionExceptionConverter();
    }

    @Bean
    public SysExceptionConverter sysExceptionConverter() {
        return new SysExceptionConverter();
    }

    @Bean
    public TypeMismatchExceptionConverter typeMismatchExceptionConverter() {
        return new TypeMismatchExceptionConverter();
    }

    @Bean
    public UnexpectedTypeExceptionConverter unexpectedTypeExceptionConverter() {
        return new UnexpectedTypeExceptionConverter();
    }

    @Bean
    @ConditionalOnProperty(value = "op-logs.enabled", matchIfMissing = true)
    public OpLogAspector opLogAspector() {
        return new OpLogAspector();
    }

    @Bean
    public OrderTimeLineLogAspect orderTimeLineLogAspect() {
        return new OrderTimeLineLogAspect();
    }

    @Bean
    public OpLogTimerListener opLogTimerListener() {
        return new OpLogTimerListener();
    }

    @Bean
    public AuthOpLogType authOpLogType() {
        return new AuthOpLogType();
    }

    @Bean
    public SystemOpLogType systemOpLogType() {
        return new SystemOpLogType();
    }

    @Bean
    public OtherOpLogType otherOpLogType() {
        return new OtherOpLogType();
    }

    @Override
    public void setEnvironment(Environment environment) {
        log.info("web-starter加载完成");
    }

    @Bean
    public SysDeptNodeType sysDeptNodeType() {
        return new SysDeptNodeType();
    }
}
